import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:management/app/cons.dart';
import 'package:management/model/pakage_model.dart';
import 'package:management/utils/convert.dart';

///快递包裹签收页面
@deprecated
class PackageSignFor extends StatefulWidget {
  @override
  _PackageSignForState createState() => _PackageSignForState();
}

class _PackageSignForState extends State<PackageSignFor> {
  final TextStyle _textStyle = TextStyle(
    fontSize: Dimension.fontSize18.sp,
    fontWeight: FontWeight.bold,
  );
  double titleWidth = (1.sw - 100) / 10;
  List<PackageModel> data = [];

  _buildAppBar() => AppBar(
        leading: BackButton(
          color: Colors.black,
          onPressed: () => Navigator.of(context).pop(),
        ),
        backgroundColor: Colors.greenAccent,
        title: Text(
          "快递签收",
          style: TextStyle(
              fontSize: 22.sp,
              fontWeight: FontWeight.bold,
              color: Colors.black),
        ),
        centerTitle: true,
      );

  _buildBody() => Container(
        // color: Colors.greenAccent,
        padding: EdgeInsets.all(20),
        child: Column(
          children: [
            Container(
              // color: Colors.grey,
              height: 30,
              child: Row(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Container(
                    width: titleWidth,
                    alignment: Alignment.center,
                    child: Text(
                      "收件日期",
                      style: _textStyle,
                    ),
                  ),
                  Container(
                    width: titleWidth,
                    alignment: Alignment.center,
                    child: Text(
                      "快递公司",
                      style: _textStyle,
                    ),
                  ),
                  Container(
                    width: titleWidth,
                    alignment: Alignment.center,
                    child: Text(
                      "快递单号",
                      style: _textStyle,
                    ),
                  ),
                  Container(
                    width: titleWidth,
                    alignment: Alignment.center,
                    child: Text(
                      "寄件人姓名",
                      style: _textStyle,
                    ),
                  ),
                  Container(
                    width: titleWidth,
                    alignment: Alignment.center,
                    child: Text(
                      "收件人姓名",
                      style: _textStyle,
                    ),
                  ),
                  Container(
                    width: titleWidth,
                    alignment: Alignment.center,
                    child: Text(
                      "代收人",
                      style: _textStyle,
                    ),
                  ),
                  Container(
                    width: titleWidth,
                    alignment: Alignment.center,
                    child: Text(
                      "领件时间",
                      style: _textStyle,
                    ),
                  ),
                  Container(
                    width: titleWidth,
                    alignment: Alignment.center,
                    child: Text(
                      "领件人",
                      style: _textStyle,
                    ),
                  ),
                  Container(
                    width: titleWidth,
                    alignment: Alignment.center,
                    child: Text(
                      "备注",
                      style: _textStyle,
                    ),
                  ),
                ],
              ),
            ),
            Divider(),
            Expanded(
              child: PackageList(
                list: this.data,
                textWidth: titleWidth,
                onItemClickListener: (context, index) {
                  setState(() {
                    this.data.removeAt(index);
                  });
                },
              ),
            ),
          ],
        ),
      );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _buildAppBar(),
      body: _buildBody(),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          setState(() {
            data.add(PackageModel(
                receiveTime: Convert.currentTime(),
                courierCompany: "京东",
                trackingNumber: "1234",
                senderName: "无",
                consigneeName: "王五",
                collector: "无",
                pickUpTime: Convert.currentTime(),
                recipient: "王五",
                remarks: "xxx"));
          });
        },
      ),
    );
  }
}

class PackageList extends StatelessWidget {
  const PackageList({
    Key? key,
    this.list,
    this.textWidth,
    this.onItemClickListener,
  }) : super(key: key);

  ///存放数据
  final List<PackageModel>? list;

  final double? textWidth;

  ///点击事件监听
  final OnItemClickListener? onItemClickListener;

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemBuilder: (context, index) => _buildItem(context, index),
      itemCount: list!.length,
    );
  }

  _buildItem(context, index) => Card(
        elevation: 6,
        child: InkWell(
          child: Container(
            height: 60,
            // padding: EdgeInsets.only(left: 20, right: 20),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(
                  width: textWidth,
                  alignment: Alignment.center,

                  child: Text(list![index].receiveTime!), //收件日期
                ),
                Container(
                  width: textWidth,

                  alignment: Alignment.center,
                  child: Text(list![index].courierCompany!), //快递公司
                ),
                Container(
                  width: textWidth,

                  alignment: Alignment.center,
                  // margin: EdgeInsets.only(left: 20),
                  child: Text(list![index].trackingNumber!), //快递单号
                ),
                Container(
                  width: textWidth,

                  alignment: Alignment.center,

                  child: Text(list![index].senderName!), //寄件人姓名
                ),
                Container(
                  // margin: EdgeInsets.only(left: 20),
                  width: textWidth,
                  alignment: Alignment.center,
                  child: Text(list![index].consigneeName!), //收件人姓名
                ),
                Container(
                  width: textWidth,

                  alignment: Alignment.center,
                  child: Text(list![index].collector!), //代收人
                ),
                Container(
                  width: textWidth,

                  alignment: Alignment.center,
                  child: Text(list![index].pickUpTime!), //领件时间
                ),
                Container(
                  width: textWidth,

                  alignment: Alignment.center,
                  child: Text(list![index].recipient!), //领件人
                ),
                Container(
                  width: textWidth,
                  alignment: Alignment.center,
                  child: Text(list![index].remarks!), //备注
                ),
              ],
            ),
          ),
          onTap: () => this.onItemClickListener!(context, index),
        ),
      );
}

typedef OnItemClickListener = void Function(BuildContext context, int index);
